/*
 * Copyright (C) 2013 Chen Hui <calmer91@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.harmonyos.fishbun;

import com.harmonyos.fishbun.util.ImageUtil;
import com.sangcomz.fishbun.adapter.image.impl.GlideAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.util.*;

/**
 * Created by sangc on 2015-11-06.
 */
public class ImageAdapter extends BaseItemProvider {

    private AbilitySlice context;
    private ImageController imageController;
    private List<Uri> imagePaths = new ArrayList<>();

    public ImageAdapter(AbilitySlice context, ImageController imageController, List<Uri> imagePaths) {
        this.context = context;
        this.imageController = imageController;
        this.imagePaths = imagePaths;
    }

    @Override
    public int getCount() {
        return imagePaths.size();
    }

    @Override
    public Uri getItem(int position) {
        return imagePaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer container) {
        Component cpt;
        ViewHolder holder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(container.getContext()).parse(ResourceTable.Layout_item, null, false);
            holder = new ViewHolder(cpt);
            cpt.setTag(holder);
        } else {

            cpt = component;
            holder = (ViewHolder) component.getTag();
        }
        bindHolder(holder,position);
        return cpt;
    }

    private void bindHolder(ViewHolder holder,int position) {
        Uri imagePath = imagePaths.get(position);
        ImageUtil.loadImage(context, holder.imageView, imagePath);
        holder.imageView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                imageController.setImgMain(imagePath);
            }
        });
    }

    public void changePath(ArrayList<Uri> imagePaths) {
        this.imagePaths = imagePaths;
        imageController.setImgMain(imagePaths.get(0));
        notifyDataChanged();
    }

    class ViewHolder {

        private Image imageView;

        ViewHolder(Component itemView) {
            this.imageView = (Image) itemView.findComponentById(ResourceTable.Id_img_item);
        }
    }
}